package pl.jtb.ctl.tabory.ui.re.ui.tr;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.FormToolkit;

import pl.jtb.ctl.tabory.dao.data.vo.VagonVO;

public class TripReportDialog extends Dialog {

    protected Shell shell;
    private final FormToolkit formToolkit = new FormToolkit(Display.getDefault());
    private Button okButton;
    private Button cancelButton;

    private List<VagonVO> listVagons;

    private int result = Window.CANCEL;
    private DateTime fieldDateFrom;
    private DateTime fieldDateUntil;
    private Combo fieldVagons;

    private Map<String, VagonVO> mapVagons;

    private Date resultDateFrom;
    private Date resultDateUntil;
    private VagonVO resultVagon;

    public TripReportDialog(Shell parent, List<VagonVO> listVagons) {
        super(parent, SWT.TITLE | SWT.BORDER | SWT.APPLICATION_MODAL);
        this.listVagons = listVagons;
    }

    public int open() {
        createContents();
        fillContents();
        shell.open();
        shell.layout();
        Display display = getParent().getDisplay();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        return result;
    }

    private void createContents() {
        shell = new Shell(getParent(), getStyle());
        shell.setSize(300, 260);
        shell.setText(getText());
        shell.setLayout(new GridLayout(1, false));

        Form form = formToolkit.createForm(shell);
        form.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
        formToolkit.paintBordersFor(form);
        form.getBody().setLayout(new GridLayout(2, false));

        formToolkit.createLabel(form.getBody(), "nr wagonu: ", SWT.NONE);
        fieldVagons = new Combo(form.getBody(), SWT.READ_ONLY);
        fieldVagons.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1));
        formToolkit.adapt(fieldVagons);
        formToolkit.paintBordersFor(fieldVagons);

        formToolkit.createLabel(form.getBody(), "data poczatkowa: ", SWT.NONE);
        fieldDateFrom = new DateTime(form.getBody(), SWT.DROP_DOWN | SWT.BORDER);
        formToolkit.adapt(fieldDateFrom);
        formToolkit.paintBordersFor(fieldDateFrom);

        formToolkit.createLabel(form.getBody(), "data koncowa:", SWT.NONE);
        fieldDateUntil = new DateTime(form.getBody(), SWT.DROP_DOWN | SWT.BORDER);
        formToolkit.adapt(fieldDateUntil);
        formToolkit.paintBordersFor(fieldDateUntil);

        Composite composite = formToolkit.createComposite(form.getBody(), SWT.NONE);
        composite.setLayout(new GridLayout(3, false));
        composite.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, true, false, 3, 1));
        formToolkit.paintBordersFor(composite);

        Label spacer = formToolkit.createLabel(composite, "", SWT.NONE);
        spacer.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

        okButton = formToolkit.createButton(composite, "save", SWT.NONE);
        okButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                try {
                    storeValue();
                } catch (Exception e1) {
                    ErrorDialog errorDialog = new ErrorDialog(shell, "error", e1.getMessage(), new Status(1, "edytor", e1.getMessage()), 1);
                    errorDialog.open();
                }
                result = Window.OK;
                shell.dispose();
            }
        });

        cancelButton = formToolkit.createButton(composite, "cancel", SWT.NONE);
        cancelButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                shell.dispose();
            }
        });

    }

    private void fillContents() {
        mapVagons = new LinkedHashMap<String, VagonVO>();
        for (VagonVO vagon : listVagons) {
            fieldVagons.add(vagon.getInternalId());
            mapVagons.put(vagon.getInternalId(), vagon);
        }
    }

    protected void storeValue() {
        Calendar startCalendar = new GregorianCalendar();
        startCalendar.set(Calendar.YEAR, fieldDateFrom.getYear());
        startCalendar.set(Calendar.MONTH, fieldDateFrom.getMonth());
        startCalendar.set(Calendar.DAY_OF_MONTH, fieldDateFrom.getDay());
        resultDateFrom = startCalendar.getTime();

        Calendar stopCalendar = new GregorianCalendar();
        stopCalendar.set(Calendar.YEAR, fieldDateUntil.getYear());
        stopCalendar.set(Calendar.MONTH, fieldDateUntil.getMonth());
        stopCalendar.set(Calendar.DAY_OF_MONTH, fieldDateUntil.getDay());
        resultDateUntil = stopCalendar.getTime();

        resultVagon = listVagons.get(fieldVagons.getSelectionIndex());
    }

    public Date getResultDateFrom() {
        return resultDateFrom;
    }

    public Date getResultDateUntil() {
        return resultDateUntil;
    }

    public VagonVO getResultVagon() {
        return resultVagon;
    }

}
